﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Catering.DisplayBoards
{
    public partial class SafetyStewardBoard : System.Web.UI.Page
    {
        Catering_Rules.ClassInfo tClassInfo = new Catering_Rules.ClassInfo();//班组管理
        Catering_Rules.AttendanceRecord tAttendanceRecord = new Catering_Rules.AttendanceRecord();//日常执勤管理
        List<Catering_Model.AttendanceRecord> AttendanceRecordList = new List<Catering_Model.AttendanceRecord>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RepDataBind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void RepDataBind()
        {
            DateTime SearchDateTime = DateTime.Now.AddDays(-1);
            if (Request.QueryString["Date"] != null)
            {
                SearchDateTime = Convert.ToDateTime(Request.QueryString["Date"]);
            }

            //获取当日 日报数据
            AttendanceRecordList = tAttendanceRecord.GetModelList("(temp1='1' or temp2='1') and[Date]=CAST('" + SearchDateTime.ToShortDateString() + "' as datetime)");

            //波西作业区
            this.Rep_1.DataSource = this.GetStewardSafetyByAreaID(1);
            this.Rep_1.DataBind();

            //辽东作业区
            this.Rep_2.DataSource = this.GetStewardSafetyByAreaID(2);
            this.Rep_2.DataBind();

            //渤南作业区
            this.Rep_4.DataSource = this.GetStewardSafetyByAreaID(4);
            this.Rep_4.DataBind();

            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, Page.GetType(), "script", "<script>$(function () {$(\".Table tr:odd\").addClass(\"Table_Tr_odd\");});</script>", false);
        }

        /// <summary>
        /// 根据作业区ID获取安全员与管事数据列表
        /// </summary>
        public List<StewardAanSafety> GetStewardSafetyByAreaID(int AreaID)
        {
            //绑定数据源
            List<StewardAanSafety> list = new List<StewardAanSafety>();
            list.Clear();
            List<Catering_Model.ClassInfo> ClassInfoList = tClassInfo.GetModelList("AreaID=" + AreaID);
            foreach (Catering_Model.ClassInfo eClassInfo in ClassInfoList)
            {
                Catering_Model.AttendanceRecord eAttendanceRecord = AttendanceRecordList.Where(p => p.temp1 == "1" && p.ClassID == eClassInfo.id).FirstOrDefault(); //获取管事人员
                StewardAanSafety Item = new StewardAanSafety();
                Item.CName = eClassInfo.CName;
                Item.StewardName = (eAttendanceRecord == null ? "" : eAttendanceRecord.PName);
                eAttendanceRecord = AttendanceRecordList.Where(p => p.temp2 == "1" && p.ClassID == eClassInfo.id).FirstOrDefault(); //获取安全员
                Item.SafetyName = (eAttendanceRecord == null ? "" : eAttendanceRecord.PName);
                list.Add(Item);
            }

            return list;
        }

        //定时刷新页面
        protected void Btn_Ref_Click(object sender, EventArgs e)
        {
            this.RepDataBind();
        }
    }

    /// <summary>
    /// 安全员与管事实体
    /// </summary>
    public class StewardAanSafety
    {
        /// <summary>
        /// 班组名称
        /// </summary>
        public string CName { get; set; }

        /// <summary>
        /// 管事
        /// </summary>
        public string StewardName { get; set; }

        /// <summary>
        /// 安全员
        /// </summary>
        public string SafetyName { get; set; }
    }
}